﻿using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using VideoMeetting.Base.DomainModel;

namespace VideoMeettingServer
{
    /// <summary>
    /// 一个房间一个UdpServer
    /// </summary>
    public class RoomServer
    {
        private MeettingRoom _room;
        private List<IPEndPoint> _clients;
        private UdpClient _udpClient;
        private IPEndPoint _localEndPoint;
        private bool _isRunning = false;

        public RoomServer(MeettingRoom room)
        {
            _clients = new List<IPEndPoint>();
            _room = room;
            _localEndPoint = new IPEndPoint(IPAddress.Parse(_room.UdpAddress), _room.UdpPort);
        }

        public void Start()
        {
            _udpClient = new UdpClient(_localEndPoint);
            _isRunning = true;
        }

        public void Stop()
        {
            _isRunning = false;
        }

        private async void ReceiveLoop()
        {
            while (_isRunning)
            {
                var result = await _udpClient.ReceiveAsync();

                //todo：命令框架设计
            }

            _udpClient.Close();
        }
    }
}